package com.person.jiaiplan.controller;

import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.person.jiaiplan.pojo.R;
import com.person.jiaiplan.pojo.dto.AddPlanInfoReqDTO;
import com.person.jiaiplan.pojo.dto.ChangePlanStatusReqDTO;
import com.person.jiaiplan.pojo.dto.PlanInfoReqDTO;
import com.person.jiaiplan.pojo.dto.PlanInfoResDTO;
import com.person.jiaiplan.pojo.entity.ScheduleLogs;
import com.person.jiaiplan.service.ScheduleCommonAppService;
import com.person.jiaiplan.service.ScheduleCommonQueryService;
import com.person.jiaiplan.service.ScheduleLogsService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 计划设置
 */
@AllArgsConstructor
@RestController
@RequestMapping("scheduleSetting")
@Validated
public class ScheduleSettingController {

    @Autowired
    private ScheduleLogsService scheduleLogsService;

    @Autowired
    private ScheduleCommonQueryService scheduleCommonQueryService;

    @Autowired
    private ScheduleCommonAppService scheduleCommonAppService;


    @PostMapping("/query")
    public R<IPage<PlanInfoResDTO>> getPlanInfo(@RequestBody PlanInfoReqDTO reqDTO) {
        return R.ok(scheduleCommonQueryService.queryPlanInfo(reqDTO));
    }

    @RequestMapping("/add")
    public R<Boolean> addPlan(@Validated @RequestBody AddPlanInfoReqDTO addPlanInfoReqDTO) {
        return R.ok(scheduleLogsService.addPlan(addPlanInfoReqDTO));
    }

    @DeleteMapping("/delete")
    public R<Boolean> deletePlan(@RequestParam(name = "planId") String id) {
        Console.log(id);
        return R.ok(scheduleCommonAppService.deletePlan(Long.valueOf(id)));
    }

    @PostMapping("/update")
    public R<PlanInfoResDTO> updatePlanInfo(@RequestBody PlanInfoResDTO planInfoResDTO) {
        return R.ok(scheduleLogsService.updatePlanInfo(planInfoResDTO));
    }

    /**
     * 修改计划状态
     *
     * @return
     */
    @PostMapping("change/planStatus")
    public R<Boolean> changePlanStatus(@RequestBody ChangePlanStatusReqDTO req) {
        ScheduleLogs scheduleLogs = scheduleLogsService.getById(req.getScheduleId());
        if (scheduleLogs == null) {
            return R.failed("计划不存在");
        } else {
            scheduleLogs.setPlanStatus(req.getPlanStatus());
            scheduleLogsService.updateById(scheduleLogs);
        }
        return R.ok(true);

    }
}
